package com.bot.edu.applysystem.applyinfo.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bot.edu.noticessystem.notices.entity.Notices;
import com.bot.edu.noticessystem.notices.service.INoticesService;
import com.bot.edu.utils.SnowFlake;
import com.bot.edu.utils.ToNotices;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.bot.edu.applysystem.applyinfo.entity.ApplyInfo;
import com.bot.edu.applysystem.applyinfo.service.IApplyInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 报名信息表
 * @Author: jeecg-boot
 * @Date:   2022-04-02
 * @Version: V1.0
 */
@Api(tags="报名信息表")
@RestController
@RequestMapping("/applyinfo/applyInfo")
@Slf4j
public class ApplyInfoController extends JeecgController<ApplyInfo, IApplyInfoService> {
	@Autowired
	private IApplyInfoService applyInfoService;

     @Autowired
     private INoticesService noticesService;
	
	/**
	 * 分页列表查询
	 *
	 * @param applyInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "报名信息表-分页列表查询")
	@ApiOperation(value="报名信息表-分页列表查询", notes="报名信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ApplyInfo>> queryPageList(ApplyInfo applyInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ApplyInfo> queryWrapper = QueryGenerator.initQueryWrapper(applyInfo, req.getParameterMap());
		Page<ApplyInfo> page = new Page<ApplyInfo>(pageNo, pageSize);
		IPage<ApplyInfo> pageList = applyInfoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param applyInfo
	 * @return
	 */
	@AutoLog(value = "报名信息表-添加")
	@ApiOperation(value="报名信息表-添加", notes="报名信息表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ApplyInfo applyInfo) {
		String id = SnowFlake.getId();
		applyInfo.setApplyId(id);
		applyInfoService.save(applyInfo);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param applyInfo
	 * @return
	 */
	@AutoLog(value = "报名信息表-编辑")
	@ApiOperation(value="报名信息表-编辑", notes="报名信息表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ApplyInfo applyInfo) {
		applyInfoService.updateById(applyInfo);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "报名信息表-通过id删除")
	@ApiOperation(value="报名信息表-通过id删除", notes="报名信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		applyInfoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "报名信息表-批量删除")
	@ApiOperation(value="报名信息表-批量删除", notes="报名信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.applyInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "报名信息表-通过id查询")
	@ApiOperation(value="报名信息表-通过id查询", notes="报名信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ApplyInfo> queryById(@RequestParam(name="id",required=true) String id) {
		ApplyInfo applyInfo = applyInfoService.getById(id);
		if(applyInfo==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(applyInfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param applyInfo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ApplyInfo applyInfo) {
        return super.exportXls(request, applyInfo, ApplyInfo.class, "报名信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ApplyInfo.class);
    }


	 /**
	  * 修改报名状态
	  */
	 @GetMapping(value = "/upStatus/{id}/{newApplyStatus}")
	 public Result<?> upstatusById(@PathVariable("id") String id,
								   @PathVariable("newApplyStatus") Integer newApplyStatus) {
		 ApplyInfo applyInfo = new ApplyInfo();
		 applyInfo.setApplyStatusId(newApplyStatus);
		 QueryWrapper<ApplyInfo> queryWrapper = new QueryWrapper<>();
		 queryWrapper.lambda().eq(ApplyInfo::getId,id);
		 applyInfoService.update(applyInfo,queryWrapper);
         Notices notices = ToNotices.toNotices( "您的报名信息已处理，请在通知中查看并重新登陆");
         noticesService.save(notices);
		 return Result.OK("修改成功！！");
	 }

     /**
      * 填写报名信息
      * @param applyInfo
      * @return
      */
	 @RequestMapping("/addApplyInFor")
     public Result<?> addApplyInFor(@RequestBody ApplyInfo applyInfo){
	     applyInfo.setApplyId(SnowFlake.getId());
	     applyInfo.setId(SnowFlake.getId());
	     applyInfoService.save(applyInfo);
	     return Result.ok();
     }
}
